import { queryNotices, getNoticesById } from '../services/api';
import { message } from 'antd';


export default {
  namespace: 'global',

  state: {
    collapsed: false,
    notices: null,
    sysTotal: null,//消息
    applyTotal: null,//待办
  },

  effects: {
    *fetchNotices(_, { call, put }) {
      const data = yield call(queryNotices);
      // console.log("data121212", data)
      if(data&&data.result&&data.result.code == 200){
        yield put({
          type: 'save',
          payload: {
            notices: data.data.messageList,
            sysTotal: data.data.total.sysTotal,
            applyTotal: data.data.total.applyTotal,
          },
        });
        yield put({
          type: 'saveNotices',
          payload: data.data.messageList,
        });
        yield put({
          type: 'user/changeNotifyCount',
          payload: data.data.total.total,        
        });
      }   
    },

    *getNoticesById({ payload, callback }, { call, put }) {
      const data = yield call(getNoticesById, payload);
      // console.log("getNoticesById", data)
      // yield put({
      //   type: 'saveNotices',
      //   payload: data,
      // });
      if (callback) callback();
    },

    *clearNotices({ payload }, { put, select }) {
      yield put({
        type: 'saveClearedNotices',
        payload,
      });
      const count = yield select(state => state.global.notices.length);
      const sysTotal = yield select(state => state.global.notices.length);
      const applyTotal = yield select(state => state.global.notices.length);
      yield put({
        type: 'user/changeNotifyCount',
        // payload: count,
        payload: {
          sysTotal,
          applyTotal
        },
      });
    },

    //去掉已读消息
    // *updateMessageRead({ payload, callback }, { select, call, put }) {
    //   yield put({
    //     type: 'saveMessageStatus',
    //     payload,
    //   });
    //   if (callback) callback();
    // },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    saveNotices(state, { payload }) {
      // console.log("saveNotices(state, { payload })", payload)
      return {
        ...state,
        notices: payload,
      };
    },
    saveClearedNotices(state, { payload }) {
      return {
        ...state,
        notices: state.notices.filter(item => item.type !== payload),
      };
    },

    // saveMessageStatus(state, { payload }) {
    //   const { item } = payload;
    //   const messageList = state.notices;
    //   const newMessageList = messageList.filter(
    //     oldItem => (oldItem.id !== item.id )
    //   );
    //   // console.log("newMessageList", newMessageList)
    //   return {
    //     ...state,
    //     notices: newMessageList,
    //   };
    // },
  },

  subscriptions: {
    setup({ history }) {
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      return history.listen(({ pathname, search }) => {
        if (typeof window.ga !== 'undefined') {
          window.ga('send', 'pageview', pathname + search);
        }
      });
    },
  },
};
